'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {

    // 返回审核数据
    async index() {
        const { ctx, app } = this;
        const { check, city, area, text, province } = ctx.query;
        let list = []
        if (check !== 'all') {
            list = await app.mysql.select('audit', {
                where: {
                    state: check
                }
            })
        } else {
            list = await app.mysql.select('audit')
        }
        if (text) list = list.filter(v => {
            if (v.title.includes(text)) {
                return v
            }
        })
        if (province) list = list.filter(v => {
            if (v.city.includes(province)) {
                return v
            }
        })
        if (city) list = list.filter(v => {
            if (v.city.includes(city)) {
                return v
            }
        })
        if (area) list = list.filter(v => {
            if (v.city.includes(area)) {
                return v
            }
        })
        list = list.map(v => {
            return {...v, key: v.id }
        })
        ctx.body = {
            code: 1,
            list
        }
    }

    // 删除审核数据
    async del() {
        const { ctx, app } = this;
        await app.mysql.delete('audit', ctx.query)
        ctx.body = {
            code: 1
        }
    }
}

module.exports = HomeController;